package controllers

import (
	"github.com/gin-gonic/gin"
	"go-admin-official/guard"
	"go-admin-official/models"
	"go-admin-official/modules/auth"
	"net/http"
)

type ThemePage struct {
	User    models.UsersModel
	List    []map[string]interface{}
	Count   int
	HasMore bool
}

func Themes(ctx *gin.Context) {
	user, _ := auth.Filter(ctx)
	param := guard.GetThemesParam(ctx)
	list, count, hasMore := models.Theme().List(param, false)
	ctx.HTML(http.StatusOK, "themes.tmpl", ThemePage{
		User:    user,
		List:    list,
		Count:   count,
		HasMore: hasMore,
	})
}

func ApiThemes(ctx *gin.Context) {
	param := guard.GetThemesParam(ctx)
	list, count, hasMore := models.Theme().List(param, true)
	ctx.JSON(http.StatusOK, gin.H{
		"code": 0,
		"data": gin.H{
			"list":     list,
			"count":    count,
			"has_more": hasMore,
		},
	})
}

type ThemeDetailPage struct {
	User  models.UsersModel
	Theme models.ThemeModel
}

func ThemeDetail(ctx *gin.Context) {
	user, _ := auth.Filter(ctx)
	uuid := ctx.Param("uuid")
	lang := ctx.Query("lang")
	if lang == "" {
		lang = "cn"
	}
	ctx.HTML(http.StatusOK, "theme_detail.tmpl", ThemeDetailPage{
		User:  user,
		Theme: models.Theme().FindByUUID(uuid, lang),
	})
}

type ThemesBuyPage struct {
	User    models.UsersModel
	List    []map[string]interface{}
	Count   int
	HasMore bool
}

func ThemesBuy(ctx *gin.Context) {
	user, _ := auth.Filter(ctx)
	param := guard.GetThemesBuyParam(ctx)
	list, count, hasMore := models.UserBuyThemes().List(param)
	ctx.HTML(http.StatusOK, "user_buy_theme.tmpl", ThemesBuyPage{
		User:    user,
		List:    list,
		Count:   count,
		HasMore: hasMore,
	})
}
